package pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler;

import java.io.Serializable;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.widgets.Display;

import pl.edu.mimuw.irs.core.model.design.net.IRSNode;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.command.OverwriteNodeCommand;
import pl.edu.mimuw.irs.rcp.util.dnd.IRSNodeTransfer;

public class PasteHandler extends AbstractNodeHandler {

	// *** Attributes

	// *** Methods
	
	@Override
	public Object execute(ExecutionEvent event, CommandStack stack, IRSNode node) {

		/* Getting clipboard contents */
		Clipboard clipboard = new Clipboard(Display.getCurrent());
		IRSNodeTransfer transfer = IRSNodeTransfer.getInstance();
		
		/* CopyHandler gives us Serializable[] containing IRSNode. */
		Serializable[] contents = (Serializable[]) clipboard.getContents(transfer);
		if (contents == null || contents.length == 0) return null;
		
		/* Here we have node which we copied. */
		IRSNode transferred = (IRSNode)contents[0];
		
		Command command = new OverwriteNodeCommand(transferred, node);
		stack.execute(command);
		
		return null;
	}
}
